import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: { title: '智慧课堂 - 主页' }
  },
  {
    path: '/3d-course-list',
    name: 'Course3DList',
    component: () => import('@/views/3d/Course3DList.vue'),
    meta: { title: '3D课程列表' }
  },
  {
    path: '/3d-classroom',
    name: 'Course3DClassroom',
    component: () => import('@/views/3d/Course3DClassroom.vue'),
    meta: { title: '3D互动课堂' }
  },
  {
    path: '/course-management',
    name: 'CourseManagement',
    component: () => import('@/views/CourseManagement.vue'),
    meta: { title: '智慧课程管理' }
  },
  {
    path: '/data-sync-test',
    name: 'DataSyncTest',
    component: () => import('@/views/DataSyncTest.vue'),
    meta: { title: '数据联动测试' }
  },
  {
    path: '/ppt-parser-test',
    name: 'PPTParserTest',
    component: () => import('@/views/PPTParserTest.vue'),
    meta: { title: 'PPT解析功能测试' }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面未找到' }
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // 解决刷新页面404的问题
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

// 路由错误处理
router.onError((error) => {
  console.error('路由错误:', error)
})

export default router 